﻿Imports AFS.Aspnet.Collaboration.Site.AppRegistryType
Imports AFS.Aspnet.Collaboration.Site.MainTypesAndEnums
Imports AFS.Collab.MainEnumsAndTypes.MainTypesAndEnums
Imports AFS.Collab.MainEnumsAndTypes

Module AppRegistration

    Private Function CreateAppRegisterFile() As XDocument
        If System.IO.File.Exists(MyFile("regapp.xml")) Then
            Return XDocument.Load(MyFile("regapp.xml"))
        Else
            Dim xdoc As New XDocument
            Dim Root As XElement = <appreg>

                                   </appreg>
            xdoc.Add(Root)
            xdoc.Save(MyFile)
            Return xdoc
        End If
    End Function

    Function RegisterApp(ByVal appid As String, ByVal typeofApp As enumTypeofApps, ByVal channel As String) As Boolean
        Dim successWrite = AddPushRegElement(appid, typeofApp, channel)
        Dim successRegister = False
        REM hier dann auf den pusher service zugreifen und die app mit den nötigen daten registrieren
        REM erst wenn beide seiten/functions true zurückgeben ist alles für metro registriert.
        If successRegister And successWrite Then
            Return True
        Else
            Return False
        End If
    End Function

    Function UnRegisterApp(ByVal appid As String, ByVal typeofApp As enumTypeofApps) As Boolean
        Dim successWrite = RemoveAppRegFromXML(appid, typeofApp)
        Dim successPusher As Boolean = True
        REM dim successPusher = hier die rückgabe vom pushersvc 
        If successPusher And successWrite Then
            Return True
        Else
            Return False
        End If
    End Function


    Private Function AddPushRegElement(ByVal appid As String, ByVal TypeOfApp As enumTypeofApps, ByVal CSecret As String) As Boolean
        Dim xdoc = CreateAppRegisterFile()

        Try
            Dim elements = From b In xdoc.Root.Elements
                           Where b.<appid>.Value = appid
                           Select b

            If elements(0) Is Nothing Then


                Dim newElement As XElement = <reg>
                                                 <appid><%= appid %></appid>
                                                 <typeof><%= TypeOfApp %></typeof>
                                                 <channel><%= CSecret %></channel>
                                             </reg>
                xdoc.Root.Add(newElement)
                xdoc.Save(MyFile)
            Else
                RemoveAppRegFromXML(appid, TypeOfApp)
                AddPushRegElement(appid, TypeOfApp, CSecret)
            End If
            Return True
        Catch
            Return False
        End Try

    End Function


    Private Function RemoveAppRegFromXML(ByVal tag As String, ByVal typeofapp As enumTypeofApps) As Boolean
        Try
            Dim xdoc = CreateAppRegisterFile()

            Dim user = From a In xdoc.Root.Elements
                       Where a.<appid>.Value = tag And a.<typeof>.Value = typeofapp
                       Select a



            If user(0) IsNot Nothing Then
                user(0).Remove()
                xdoc.Save(MyFile)
            End If
            Return True
        Catch
            Return False
        End Try
    End Function

    Private Function IntToEnumConverter(ByVal int As String) As enumTypeofApps
        Select Case int
            Case "0", 0
                Return enumTypeofApps.metro
            Case "1", 1
                Return enumTypeofApps.phone
            Case "2", 2
                Return enumTypeofApps.phone
        End Select
    End Function

    Function ReadAllAppRegsFromXMLFile() As List(Of AppRegistryType)

        Dim retlist As New List(Of AppRegistryType)
        Dim xdoc = CreateAppRegisterFile()
        For Each a In xdoc.Root.Elements
            Dim nuser As New AppRegistryType
            With nuser
                .AppId = a.<appid>.Value.ToString
                .TypeOfApp = IntToEnumConverter(a.<typef>.Value.ToString)
                .Channel = a.<channel>.Value.ToString
            End With
            retlist.Add(nuser)
        Next
        Return retlist
    End Function
End Module


Public Class AppRegistryType
    Property AppId As String
    Property TypeOfApp As MainTypesAndEnums.enumTypeofApps
    Property Channel As String
End Class